package com.wc.AlgoOJ_train.train3_算法大礼包.c3_贪心.AG0407_小熊食堂;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
 * @Author congege
 * @Description
 * @Date Created in 2024/2/1-3:25
 * http://43.138.190.70:8888/p/AG0407
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 50010;
    // a[i][0] 表示他原始的位置，a[i][1]表示左端点，a[i][2]表示右端点
    static int[][] a = new int[N][3];
    static int[] group = new int[N];
    // f[i]表示 a[i]分到了哪一个组
    static int[] f = new int[N];
    static int groupLen = 0;
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i][0] = i;
            a[i][1] = sc.nextInt();
            a[i][2] = sc.nextInt();
        }
        Arrays.sort(a, 1, n + 1, Comparator.comparingInt(o -> o[1]));
        for (int i = 1; i <= n; i++) {
            // 是否当前组员加入组了
            boolean flg = false;
            for (int j = 1; j <= groupLen; j++) {
                if (group[j] < a[i][1]) {
                    group[j] = a[i][2];
                    f[a[i][0]] = j;
                    flg = true;
                    break;
                }
            }
            // 说明没有组要他，他自己单独一个组
            if (!flg) {
                group[++groupLen] = a[i][2];
                f[a[i][0]] = groupLen;
            }
        }
        out.println(groupLen);
        for (int i = 1; i <= n; i++) {
            out.println(f[i]);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    boolean hasNext() {
        while (st == null || !st.hasMoreElements()) {
            String s = nextLine();
            if (s == null) {
                return false;
            }
            st = new StringTokenizer(s);
        }

        return true;
    }
}

